package no.hist.rtc.rtchatu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import android.util.Log;

public class Task extends Thread{
	private MainActivityU mau;
	private int portnr = 12345;
	private String TAG = "TAGGG";
	
	public Task(MainActivityU mau){
		this.mau = mau;
		mau.postMld("Task startet...");
	}
    	
	public void run() {
		mau.postMld("Starter doInBackground...");
		while(true){
			ServerSocket ss 	= null;
			Socket s 			= null;
			PrintWriter out 	= null;
			BufferedReader in 	= null;
			
			try{
	    		Log.i(TAG,"start server....");
	    		mau.postMld("start server....");
	            ss = new ServerSocket(portnr);
	            Log.i(TAG,"serversocket created, wait for client....");
	            mau.postMld("serversocket created, wait for client....");
	            
	            s = ss.accept(); //stor og venter
	            
	            Log.v(TAG, "client connected...");
	            mau.postMld("client connected...");
	            out = new PrintWriter(s.getOutputStream(), true);
	            out.print("klar til aa motta...");
	            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
	            String res = in.readLine();
	            
	            mau.postMld(res);
	            
			} catch (IOException e) {
	            e.printStackTrace();
	        }finally{//close sockets!!
	        	try{
	        		out.close();
	            	in.close();
	        		s.close();
	        		ss.close();
	        	}catch(Exception e){}	            	
	        }   
		}
	}
}